package com.example.demo.controller;

import com.example.demo.domain.Curriculum;
import com.example.demo.domain.Grade;
import com.example.demo.domain.Student;
import com.example.demo.service.CurriculumService;
import com.example.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
@RequestMapping("/pri/student")
public class StudentController {


    private StudentService studentService;
    @Autowired
    public void setStudentService(StudentService studentService){
        this.studentService =studentService;
    }

    private CurriculumService curriculumService;
    @Autowired
    public void setCurriculumService(CurriculumService curriculumService){
        this.curriculumService =curriculumService;
    }

    /**
     * 查询功能
     * @return
     */
    @GetMapping("/findAll")
    public String check(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                        @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize,
                        Model model){
        Integer totalPage = null;
        pageNum = (pageNum-1)*pageSize;
        List<Student> studentList = studentService.findAll(pageNum,pageSize);
        Integer pageTotal = studentService.findTotalRecord();
        model.addAttribute("studentList",studentList);
        //实行分页
        if(pageTotal % pageSize == 0){
            totalPage = pageTotal / pageSize;
        }else{
            totalPage = pageTotal / pageSize + 1;
        }
        model.addAttribute("totalPage",totalPage);
        return "student/list";
    }

    /**
     * 删除功能
     * @param request
     * @return
     */
    @GetMapping("/delete")
    public String delete(HttpServletRequest request){
        int cId = Integer.parseInt(request.getParameter("id"));
        studentService.deleteStudent(cId);
        return "redirect:/pri/student/findAll";
    }

    /**
     * 添加功能
     * @param model
     * @return
     */

    @GetMapping("/toAdd")
    public String toAdd(Model model){
        model.addAttribute("student",new Student());
        return "student/add";
    }


    @PostMapping("/add")
    public String add(Student student){
        studentService.insertStudent(student);
        return "redirect:/pri/student/findAll";
    }

    /**
     * 更新接口
     * @param request
     * @param model
     * @return
     */
    @GetMapping("/toUpdate")
    public String toUpdate(HttpServletRequest request,Model model){
        int cId = Integer.parseInt(request.getParameter("id"));
        Student student = studentService.findStudentById(cId);
        model.addAttribute("student", student);
        return "student/modify";
    }

    @PostMapping("/update")
    public String update(Student student){
        studentService.updateStudent(student);
        return "redirect:/pri/student/findAll";
    }

    /**
     * 学生信息详情
     * @param request
     * @param model
     * @return
     */
    @GetMapping("/findDetail")
    public String findByIdDetail(HttpServletRequest request,Model model){

        Integer id = Integer.parseInt(request.getParameter("id"));
        Student student = studentService.findByIdDetail(id);
        model.addAttribute("studentDetail",student);
        return "student/detail";
    }



    /**
     * 查询学生成绩, 正则维护中, 接口还没开发完全 如何才能使得集合映射到数据中去
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/findGrade")
    public String findByIdWithGrade(@RequestParam(value = "id", required = false, defaultValue = "1") Integer id,
                                    Model model){
        List<Grade> gradeList = studentService.findByIdWithGrade(id);
        if(gradeList != null){
            //这个是处理如何把课程编号转变为课程标题
            String courseNo = null;
            for(int i = 0; i < gradeList.size(); i++){
                courseNo = gradeList.get(i).getCourseNo();
                Curriculum curriculum = curriculumService.findCourseByNo(courseNo);
                gradeList.get(i).setCourseNo(curriculum.getCourseTitle());
            }
            //这个是获取姓名
            String name = (String)gradeList.get(0).getStudentList().get(0).getName();
            model.addAttribute("gradeList",gradeList);
            model.addAttribute("name", name);
            return "student/grade";
        }
        return "error";
    }

}
